package com.example.user1.util;

import com.example.user1.config.AutoConfig;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Date;
import java.util.Map;

@RequiredArgsConstructor
public class JwtTokenGenerate implements TokenGenerateUtil {
    private final RSAPrivateKey rsaPrivateKey;
    @SneakyThrows
    @Override
    public String token(Map<String, Object> claims) {
        // 创建JWT payload
        JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
        builder.issuer("yu.wang").issueTime(new Date()).expirationTime(new Date(System.currentTimeMillis() + 6 * 5 * 60 * 1000)).subject("detail");
        claims.forEach(builder::claim);
        JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID("wang.yu").build();
        // 创建JWS对象
        SignedJWT jwt = new SignedJWT(header, builder.build());

        // 使用私钥对JWT进行签名
        JWSSigner signer = new RSASSASigner(rsaPrivateKey);
        jwt.sign(signer);
        return jwt.serialize();
    }
}
